//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.vere.orm.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;

public class CFile {
    public CFile() {
    }

    public static void save(File file, String fileName) {
        InputStream in = null;
        FileOutputStream os = null;

        try {
            if (file != null) {
                String dir = fileName.substring(0, fileName.lastIndexOf(File.separatorChar));
                if (!fileExists(dir)) {
                    makeDir(dir, true);
                }

                in = new FileInputStream(file);
                os = new FileOutputStream(fileName);
                byte[] buffer = new byte[1024];
                boolean var6 = false;

                int length;
                while((length = in.read(buffer)) != -1) {
                    os.write(buffer, 0, length);
                }

                os.flush();
            }
        } catch (Exception var15) {
            var15.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }

                if (os != null) {
                    os.close();
                }
            } catch (IOException var14) {
                var14.printStackTrace();
            }

        }

    }

    public static void save(InputStream in, String fileName) {
        if (in != null) {
            FileOutputStream os = null;

            try {
                String dir = fileName.substring(0, fileName.lastIndexOf(File.separatorChar));
                if (!fileExists(dir)) {
                    makeDir(dir, true);
                }

                os = new FileOutputStream(fileName);
                byte[] buffer = new byte[1024];
                boolean var5 = false;

                int length;
                while((length = in.read(buffer)) != -1) {
                    os.write(buffer, 0, length);
                }

                os.flush();
            } catch (Exception var14) {
                var14.printStackTrace();
            } finally {
                try {
                    if (in != null) {
                        in.close();
                        in = null;
                    }

                    if (os != null) {
                        os.close();
                        os = null;
                    }
                } catch (IOException var13) {
                    var13.printStackTrace();
                }

            }

        }
    }

    public static boolean fileExists(String _sPathName) {
        File file = new File(_sPathName);
        return file.exists();
    }

    public static String extractFileName(String _sFilePathName) {
        int nPos = _sFilePathName.lastIndexOf(File.separatorChar);
        return _sFilePathName.substring(nPos + 1);
    }

    public static boolean deleteFile(String _sFilePathName) {
        File file = new File(_sFilePathName);
        return fileExists(_sFilePathName) && file.isFile() ? file.delete() : false;
    }

    public static boolean makeDir(String _sDir, boolean _bCreateParentDir) {
        File file = new File(_sDir);
        file.setWritable(true);
        return _bCreateParentDir ? file.mkdirs() : file.mkdir();
    }

    public static void makeFile(String filePath) {
        File file = new File(filePath);
        file.setWritable(true);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException var3) {
                var3.printStackTrace();
            }
        }

    }

    public static boolean deleteDir(String _sDir) {
        return deleteDir(_sDir, false);
    }

    public static boolean deleteDir(String _sDir, boolean _bDeleteChildren) {
        File file = new File(_sDir);
        if (!file.exists()) {
            return false;
        } else {
            if (_bDeleteChildren) {
                File[] files = file.listFiles();

                for(int i = 0; i < files.length; ++i) {
                    if (files[i].isDirectory()) {
                        deleteDir(files[i].getAbsolutePath(), _bDeleteChildren);
                    } else {
                        files[i].delete();
                    }
                }
            }

            return file.delete();
        }
    }

    public static String getFileSeparator() {
        return String.valueOf(File.separatorChar);
    }

    public static boolean logAppend(String content, String fileName) {
        String dir = fileName.substring(0, fileName.lastIndexOf(getFileSeparator()));
        if (!fileExists(dir)) {
            makeDir(dir, true);
        }

        new File(fileName);
        if (!fileExists(fileName)) {
            makeFile(fileName);
        }

        BufferedWriter out = null;

        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, true), "utf-8"));
            out.newLine();
            out.write(content);
            out.flush();
        } catch (Exception var14) {
            var14.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException var13) {
                var13.printStackTrace();
            }

        }

        return false;
    }
}
